//实现语音识别子服务
#include <brpc/server.h>
#include <butil/logging.h>

#include "asr.hpp"      // 语音识别模块封装
#include "etcd.hpp"     // 服务注册模块封装
#include "Logger.hpp"   // 日志模块封装
#include "speech.pb.h"  // protobuf框架代码
#include "channel.hpp"

//服务发现
DEFINE_bool(mode,false,"true-发布,false-调试");
DEFINE_string(filename,"file","要保存的文件名");
DEFINE_int32(level,0,"表示日志的输出等级");

DEFINE_string(etcdhost,"http://127.0.0.1:2379","etcd服务器");
DEFINE_string(dir,"/service","监控的服务");
DEFINE_string(callservice,"/service/speech_service","增加的目录");


int main(int argc,char* argv[])
{
    google::ParseCommandLineFlags(&argc, &argv, true);
    chatroom::InitLogger(FLAGS_mode,FLAGS_filename,FLAGS_level);
    // Discovery::ptr dclient= make_shared<Discovery>(FLAGS_etcdhost,FLAGS_dir,online,offline);
    // std::this_thread::sleep_for(std::chrono::seconds(20)); 
    // 1 创建信道服务管理对象
    auto sm = std::make_shared<chatroom::ServiceManage>();
    sm->declared(FLAGS_callservice);
    auto put_cb = std::bind(&chatroom::ServiceManage::ServiceOnline,sm.get(),std::placeholders::_1,std::placeholders::_2);
    auto off_cb = std::bind(&chatroom::ServiceManage::ServiceOffline,sm.get(),std::placeholders::_1,std::placeholders::_2);
    // std::function<void(std::string, std::string)> put_cb =
    // std::bind(&chatroom::ServiceManage::ServiceOnline, &sm, std::placeholders::_1, std::placeholders::_2);
    // std::function<void(std::string, std::string)> off_cb =
    // std::bind(&chatroom::ServiceManage::ServiceOffline, &sm, std::placeholders::_1, std::placeholders::_2);
    // 3 发现服务
    chatroom::Discovery::ptr dclient= std::make_shared<chatroom::Discovery>(FLAGS_etcdhost,FLAGS_dir,put_cb,off_cb);
    auto channel = sm->choose(FLAGS_callservice);
    if(!channel)
    {
        chatroom::Logger_ERROR("没有对应的信道提供该服务");
        std::this_thread::sleep_for(std::chrono::seconds(1)); 
        return -1;
    }
        // 2 构建对象EchnoService_stub用于Rpc调用
    chatroom::SpeechService_Stub stub(channel.get());
        // 3 进行RPC调用
        // 最好是new出来的，不然函数结束，这些参数就会被释放掉，所以要让变量在堆上
    brpc::Controller* cntl = new brpc::Controller();
    chatroom::SpeechRecognitionReq req;
    chatroom::SpeechRecognitionRsp* rsp = new chatroom::SpeechRecognitionRsp();;
        // req->set_message("hello test");
    std::string file_content;
    aip::get_file_content("16k.pcm", &file_content);
    req.set_speech_content(file_content);
    req.set_request_id("1111");
        // 同步调用RPC
    stub.SpeechRecognition(cntl,&req,rsp,nullptr);
        // // 异步调用RPC
        // auto closure = google::protobuf::NewCallback(callback,cntl,rsp);
        // stub.Echo(cntl,req,rsp,closure);
    if(cntl->Failed())
    {
        std::cout<< "RPC调用失败" <<cntl->ErrorText() <<std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(1));
        delete cntl;
        delete rsp;
        return -1;
    }
        //调用成功之后，获取响应
    if(rsp->success()==false)
    {
        std::cout<<rsp->errmsg()<<std::endl;
        delete cntl;
        delete rsp;
        return -1;
    }
    std::cout<<rsp->request_id()<<std::endl;
    std::cout<<rsp->recognition_result()<<std::endl;
    delete cntl;
    delete rsp;
    return 0;
}
